package com.yf.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登陆控制
 */
@CrossOrigin
@Controller
@ResponseBody
public class LoginController {

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String Login(String empName, String empPassword, Model model){
        System.out.println("登陆.....");
        System.out.println(empName+"....."+empPassword);
        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(empName, empPassword);

        try {
            subject.login(token);
        }  catch (UnknownAccountException uae) {
            System.out.println("账户不存在....");
            model.addAttribute("msg","账户不存在");
            return "noSuccess";
        } catch (IncorrectCredentialsException ice) {
            System.out.println("密码错误....");
            model.addAttribute("msg","密码错误");
            return "noSuccess";
        }catch (AuthenticationException ae) {
            System.out.println("其他认证异常...."+ae.getMessage());
            model.addAttribute("msg","其他异常...");
            return "noSuccess";
        }
        return "Success";
    }


}
